package cn.tedu.property.management.service.impl;

import cn.tedu.property.management.ex.ServiceException;
import cn.tedu.property.management.mapper.VisitPeopleMapper;
import cn.tedu.property.management.pojo.dto.VisitPeopleAddNewDTO;
import cn.tedu.property.management.pojo.dto.VisitPeopleUpdateDTO;
import cn.tedu.property.management.pojo.entity.VisitPeople;
import cn.tedu.property.management.pojo.vo.VisitPeopleListItemVO;
import cn.tedu.property.management.pojo.vo.VisitPeopleStandardVO;
import cn.tedu.property.management.service.IVisitPeopleService;
import cn.tedu.property.management.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 处理访客数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Service
@Slf4j
public class VisitPeopleServiceImpl implements IVisitPeopleService {

    @Autowired
    private VisitPeopleMapper visitPeopleMapper;

    @Override
    public void addNew(VisitPeopleAddNewDTO visitPeopleAddNewDTO) {
        log.debug("开始处理添加访客业务，参数为：{}",visitPeopleAddNewDTO);
        // 检查访客名称是否被占用
        String name = visitPeopleAddNewDTO.getName();
        int countByName = visitPeopleMapper.countByName(name);
        if(countByName>0){
            // 如果已经被占用，抛出异常
            String message = "添加失败,访客名称已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        VisitPeople visitPeople = new VisitPeople();
        BeanUtils.copyProperties(visitPeopleAddNewDTO,visitPeople);
        int rows = visitPeopleMapper.insert(visitPeople);
        if (rows != 1) {
            String message = "添加访客失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据id删除访客】的业务，参数：{}", id);
        //查询id是否存在
        VisitPeopleStandardVO queryResult = visitPeopleMapper.getStandardById(id);
        if(queryResult == null){
            //查询id不存在 抛出异常
            String message = "删除访客失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        int rows = visitPeopleMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除访客失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void updateInfoById(Long id, VisitPeopleUpdateDTO visitPeopleUpdateDTO) {
        log.debug("开始处理【根据id修改访客】的业务，id：{}，参数：{}", id,visitPeopleUpdateDTO);
        //查询id是否存在
        VisitPeopleStandardVO queryResult = visitPeopleMapper.getStandardById(id);
        if(queryResult == null){
            //查询id不存在 抛出异常
            String message = "修改失败,查询id不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        String name = visitPeopleUpdateDTO.getName();
        int countByName = visitPeopleMapper.countByNameAndNotId(id,name);
        if(countByName>0){
            // 如果已经被占用，抛出异常
            String message = "修改失败,访客名称已被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //没有被占用,插入数据
        // 创建VisitPeople对象
        VisitPeople visitPeople = new VisitPeople();
        //补全VisitPeople对象中的属性值:来自参数visitPeopleUpdateDTO
        BeanUtils.copyProperties(visitPeopleUpdateDTO,visitPeople);
        visitPeople.setId(id);
        log.debug("开始执行修改业务,参数:{}",visitPeople);
        int rows = visitPeopleMapper.update(visitPeople);
        if (rows != 1) {
            String message = "修改访客详情失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public VisitPeopleStandardVO getStandardById(Long id) {
        log.debug("开始处理根据ID查询数据详情");
        //查询id是否存在
        VisitPeopleStandardVO queryResult = visitPeopleMapper.getStandardById(id);
        if(queryResult == null){
            //查询id不存在 抛出异常
            String message = "查询失败,查询id不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        log.debug("查询完毕!参数为：{}",queryResult);
        return queryResult;
    }

    @Override
    public List<VisitPeopleListItemVO> list() {
        log.debug("开始处理【查询访客列表】的业务");
        return visitPeopleMapper.list();
    }
}
